#include "calculator.h"

calculator::calculator(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	this->setWindowIcon(QIcon(":/images/qicon"));
	this->setWindowTitle("计算器");
	this->setMaximumSize(250, 280);
	this->setMinimumSize(250, 280);

	QFont lineFont("微软雅黑", 15);
	ui.mainLineEdit->setFont(lineFont);

	ui.equalButton->setStyleSheet("background: blue");
}

calculator::~calculator()
{}

void calculator::on_oneButton_clicked()
{
	expression += "1";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_twoButton_clicked()
{
	expression += "2";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_threeButton_clicked()
{
	expression += "3";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_fourButton_clicked()
{
	expression += "4";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_fiveButton_clicked()
{
	expression += "5";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_sixButton_clicked()
{
	expression += "6";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_sevenButton_clicked()
{
	expression += "7";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_eightButton_clicked()
{
	expression += "8";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_nineButton_clicked()
{
	expression += "9";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_zeroButton_clicked()
{
	expression += "0";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_addButton_clicked()
{
	expression += "+";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_subButton_clicked()
{
	expression += "-";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_mulButton_clicked()
{
	expression += "*";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_divButton_clicked()
{
	expression += "/";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_leftButton_clicked()
{
	expression += "(";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_rightButton_clicked()
{
	expression += ")";
	ui.mainLineEdit->setText(expression);
}

void calculator::on_clearButton_clicked()
{
	expression.clear();
	ui.mainLineEdit->clear();
}

void calculator::on_deleteButton_clicked()
{
	expression.chop(1);
	ui.mainLineEdit->setText(expression);
}

void calculator::on_equalButton_clicked()
{
	QStack<int> s_num, s_opt;
	char opt[128] = { 0 };
	int i = 0, temp = 0, num1, num2;

	QByteArray ba = expression.toLocal8Bit();
	//strncpy_s(opt, ba.data(), sizeof(opt) - 1);
	strcpy(opt, ba.data());

	while (opt[i] != '\0' || s_opt.empty() != true)
	{
		if (opt[i] >= '0'&&opt[i] <= '9')
		{//操作数
			temp = temp * 10 + opt[i] - '0';
			i++;
			if (opt[i] < '0' || opt[i] > '9')
			{
				s_num.push(temp);
				temp = 0;
			}
		}
		else
		{//操作符
			if (s_opt.empty() == true ||								//空
				priority(opt[i]) > priority(s_opt.top()) ||		//操作符优先级大
				(s_opt.top() == '(' && opt[i] != ')'))			//'('入栈且不是')'
			{
				s_opt.push(opt[i]);
				i++;
				continue;
			}
			if (s_opt.top() == '(' && opt[i] == ')')
			{
				s_opt.pop();
				i++;
				continue;
			}
			if (priority(opt[i]) <= priority(s_opt.top()) ||
				(opt[i] == ')' && s_opt.top() != '(') ||
				(opt[i] == '\0' && s_opt.empty() != true))
			{
				char ch = s_opt.top();
				s_opt.pop();
				switch (ch)
				{
				case '+':
					num1 = s_num.top();
					s_num.pop();
					num2 = s_num.top();
					s_num.pop();
					s_num.push(num1 + num2);
					break;
				case '-':
					num1 = s_num.top();
					s_num.pop();
					num2 = s_num.top();
					s_num.pop();
					s_num.push(num1 - num2);
					break;
				case '*':
					num1 = s_num.top();
					s_num.pop();
					num2 = s_num.top();
					s_num.pop();
					s_num.push(num1 * num2);
					break;
				case '/':
					num1 = s_num.top();
					s_num.pop();
					num2 = s_num.top();
					s_num.pop();
					s_num.push(num1 / num2);
					break;
				default:
					break;
				}
			}
		}
	}
	ui.mainLineEdit->setText(QString::number(s_num.top()));
	expression.clear();
	return;
}

int calculator::priority(char ch)
{
	switch (ch)
	{
	case '(':
		return 3;
	case '*':
	case '/':
		return 2;
	case '+':
	case '-':
		return 1;
	default:
		return 0;
	}
}

